import json
import requests
import numpy as np
import calendar
import time

header = {
	"Host": "stock.xueqiu.com",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
	"Accept": "application/json, text/plain, */*",
	"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
	"Accept-Encoding": "gzip, deflate, br",
	"Referer": "https://xueqiu.com/S/SZ000912",
	"Origin": "https://xueqiu.com",
	"Connection": "keep-alive",
	"Cookie": "Hm_lvt_1db88642e346389874251b5a1eded6e3=1631162028,1631162086,1631162090,1631162117; device_id=f22a9f054403c0c6987cc97218f511d7; s=cp19vaih1o; bid=12f6cbd38037025574fa6c0b4111476f_kqaaqsph; xq_r_token=e4f3f6a2fe927671bf6292bb1b5e37f275b4ff1e; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjg3MzY0OTk2NzcsImlzcyI6InVjIiwiZXhwIjoxNjMzNTc0MjkwLCJjdG0iOjE2MzA5ODIyOTA1NjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.CE-S3i5GaoRFVDUCYZjURbMkxD3egmwoLLXA-lg17IoJq1nNOoO8FdRJKoZAmhIMudcFuWPRqfOdaItNF6Pi1uuc9TLiAhIqnPYX7YsfMMRYgpb93BQXQYIzHlfe5gGVMTx6negyOwIdznvNZlPQDMC2TcglnrDIfBSP6lFHgz-XNmYiiAfloFu56xz-A1twnNQNGvfS1ePmZA1ir3hG9VsH257W3HuHIZ_32gO1-wxc0PP9gLfKE7DNnrVut2NH2t5f0N5uOmT2SwI0LYW5txDsEkzloNTCGa1sWq0z1s6DHZ7qO7rTkDj5Bsr2Rsekrf-Eik5mu2_Sz5XRFxqMpA; u=8736499677; xq_is_login=1; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1631163711; snbim_minify=true; xq_a_token=b0d08e39a53365bc934012b1a69be0be168da130; xqat=b0d08e39a53365bc934012b1a69be0be168da130; is_overseas=0",
	"Sec-Fetch-Dest": "empty",
	"Sec-Fetch-Mode": "cors",
	"Sec-Fetch-Site": "same-site",
	"Pragma": "no-cache",
	"Cache-Control": "no-cache",
	"TE": "trailers"
}


def get_details(code_name):
    t = time.time()
    t_1 = int(round(t * 1000))

    # url = 'https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=' + code_name + '&begin=1626757885739&period=day&type=before&count=-284&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance'
    url = 'https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=' + code_name + '&begin=' + str(t_1) + '&period=day&type=before&count=-284&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance'
    resp = requests.get(url, timeout=30, headers=header)
    res = str(resp.text)
    data = json.loads(res)
    # print(data)
    day_list = data['data']['item']

    # 5天涨4天的
    up_num_5 = 0
    up_num_10 = 0
    up_num_20 = 0
    up_num_30 = 0
    # print(day_list)
    if len(day_list) > 30:
        for day in range(len(day_list) - 1, len(day_list) - 6, -1):
            if float(day_list[day][7]) >= 0:
                up_num_5 += 1
        for day in range(len(day_list) - 1, len(day_list) - 10, -1):
            if float(day_list[day][7]) >= 0:
                up_num_10 += 1
        for day in range(len(day_list) - 1, len(day_list) - 20, -1):
            if float(day_list[day][7]) >= 0:
                up_num_20 += 1
        for day in range(len(day_list) - 1, len(day_list) - 30, -1):
            if float(day_list[day][7]) >= 0:
                up_num_30 += 1
    return up_num_5, up_num_10, up_num_20, up_num_30


if __name__ == '__main__':
    up_num_5, up_num_10, up_num_20, up_num_30 = get_details('SH600308')
    print(up_num_5, up_num_10, up_num_20, up_num_30)
